import Vue from 'vue'
import Vuex from 'vuex'

Vue.use(Vuex)

export default new Vuex.Store({
  state: {
    listadd: localStorage.buhui ? JSON.parse(localStorage.buhui) : [],
  },
  getters: {
    getprice(state, payloud) {
      let total = 0;
      state.listadd.forEach(item => {
        if (item.check) {
          total += item.num * item.price * 100
        }
      })
      return total;
    },
    checked(state) {
      return state.listadd.every(item => item.check)
    }
  },
  mutations: {
    Save(state, payloud) {
      console.log(payloud);
      let index = state.listadd.findIndex(item => item.id == payloud.id);
      if (index == -1) {
        state.listadd.push(payloud);
      } else {
        state.listadd[index].num++;
        // state.listadd[index].num = state.listadd[index].num + payloud.num
      }
      localStorage.buhui = JSON.stringify(state.listadd);
    },
    Sdekl(state, payloud) {
      let index = state.listadd.findIndex(item => item.id == payloud.id);
      state.listadd.splice(index, 1);
      localStorage.buhui = JSON.stringify(state.listadd);
    },
    chengeck(satte, payloud) {
      satte.listadd.forEach(item => {
        item.check = payloud.checked;
      })
      localStorage.buhui = JSON.stringify(satte.listadd);
    }
  },
  actions: {
  },
  modules: {
  }
})
